﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

using MapCenterSystem.DataInfo;
using MapCenterSytem.Process;
using MapCenterSystem.ServiceProvider.GoogleMap;
using MapCenterSystem.Utility;
using MapCenterSystem.DefineType;

using System.Threading;
using System.Resources;
using System.Globalization;

namespace MobiMap.Admin
{
    public partial class ServiceDetail : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
    {
        ResourceManager rm;
        CultureInfo ci;

        protected void Page_Load(object sender, EventArgs e)
        {
            //Tab active
            Label lbService = (Label)Master.FindControl("tabService");
            lbService.CssClass = "tab_active";
            HtmlControl divSearchPlace = (HtmlControl)Master.FindControl("divSearchPlace");
            divSearchPlace.Controls.Clear();

            if (!IsPostBack)
            {
                int placeID = int.Parse(Request.QueryString["ID"]);
                PlaceInfo placeInfo = PlaceEx.GetPlaceById(placeID);

                lbName.Text = placeInfo.FullName;
                lbPlaceType.Text = placeInfo.Type.Description;
                lbAddress.Text = placeInfo.Address;
                lbPhone.Text = placeInfo.Phone;
                lbWebsite.Text += placeInfo.Website;
                lbDescription.Text = placeInfo.Info;
                hiddenID.Value = placeID.ToString();

                string imgDirectory = string.Empty;
                if (placeInfo.Picture == string.Empty)
                {
                    placeInfo.Picture = placeInfo.Type.Picture;
                    imgDirectory += "../Icons/PlaceType/";
                }
                else
                    imgDirectory += "../Icons/Place/";

                imgPic.Src = imgDirectory + placeInfo.Picture;
                imgPic.Alt = placeInfo.Picture;

                placeUC.FillContent(placeInfo);

                HiddenField hiddenInitMapCenter = (HiddenField)Master.FindControl("hiddenInitMapCenter");
                hiddenInitMapCenter.Value = "newcenter=latlong"
                                        + "@" + placeInfo.Location.Latitude
                                        + "@" + placeInfo.Location.Longitude;
            }

            //Language
            SetLanguage();
        }

        public void SetLanguage()
        {
            rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));
            if (Request.Cookies["language"] != null)
            {
                string language = Request.Cookies["language"].Value;
                Thread.CurrentThread.CurrentCulture = new CultureInfo(language);
                LoadString(Thread.CurrentThread.CurrentCulture);

                return;
            }

            //Defaut language is Vietnamese
            Thread.CurrentThread.CurrentCulture = new CultureInfo("vi-VN");
            ci = Thread.CurrentThread.CurrentCulture;
            LoadString(ci);
        }

        public void LoadString(CultureInfo ci)
        {
            btnEdit.Text = rm.GetString("Admin_Edit", ci);
            btnDelete.Text = rm.GetString("Admin_Delete", ci);
            btnSave.Text = rm.GetString("Admin_Update", ci);
            btnCancel.Text = rm.GetString("Admin_Cancel", ci);
            hddConfirm.Value = rm.GetString("Admin_Confirm", ci);
        }

        #region ICallbackEventHandler Members

        string callBackData ;
        public string GetCallbackResult()
        {
            return callBackData;
        }

        public void RaiseCallbackEvent(string eventArgument)
        {
            //placeUC.FillContent(placeInfo);
        }

        #endregion

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int placeID = int.Parse(hiddenID.Value);            
            PlaceEx.DeletePlace(placeID);
            Response.Write("<script type=\"text/javascript\" language=\"javascript\">alert(\"Địa điểm đã được xóa!\")</script>");
            Response.Redirect("ManageService.aspx");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int id = int.Parse(hiddenID.Value);
            string fullname = placeUC.FullName;
            string address = placeUC.Address;
            PlaceTypeInfo type = new PlaceTypeInfo();
            type.ID = int.Parse(placeUC.Type);
            Coordinate location = new Coordinate(double.Parse(placeUC.Latitude), 
                                                    double.Parse(placeUC.Longitude));
            
            string phone = placeUC.Phone;
            string website = placeUC.Website;
            string info = placeUC.Info;
            string picture = "";

            PlaceInfo place = new PlaceInfo(id, type, fullname, address, phone, website, location, info, picture);
            bool rsUpdate = PlaceEx.UpdatePlace(place);
            if (rsUpdate == true)
            {
                if (placeUC.Picture.PostedFile.FileName != "")
                {
                    picture = "icon_" + id + ".png";
                    string filename = AppDomain.CurrentDomain.BaseDirectory + @"/Icons/Place/" + picture;
                    placeUC.Picture.PostedFile.SaveAs(filename);
                    if(picture != imgPic.Alt)
                        PlaceEx.UpdatePlacePicture(id, picture);
                }
            }

            
            Response.Redirect("ServiceDetail.aspx?ID=" + id);
        }        
    }
}
